<template>
  <div class="common-layout">
    <el-container>
      <el-aside width="200px">
        <h4>后台管理系统</h4>
        <!-- 菜单  setBreadcrum('/home/'+val.path)-->
        <el-menu
          active-text-color="#ffd04b"
          background-color="#545c64"
          class="el-menu-vertical-demo"
          default-active="2"
          text-color="#fff"
          @open="handleOpen"
          @close="handleClose"
          :unique-opened="true"
        >
          <el-sub-menu
            :index="item.id + ''"
            v-for="item in menus"
            :key="item.id"
          >
            <template #title>
              <el-icon>
                <component :is="icon[item.id]"></component>
              </el-icon>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="val.id + ''"
              v-for="val in item.children"
              :key="val.id"
              @click="router.push('/homes/'+val.path); breadcrumbData.firstLevelColumn=item.authName;breadcrumbData.secondLevelColumn=val.authName"
            >
            <span>{{ val.authName}} </span> </el-menu-item
            >
          </el-sub-menu>
        </el-menu>
      </el-aside>
      
      <el-container>
        <el-header>  
         <span>欢迎您:{{ username }}</span>
          <a href @click.prevent="logout">退出登录</a>
        </el-header>
        <el-main>
          <el-card shadow="never">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/homes' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="route.path!='/homes'">{{ breadcrumbData.firstLevelColumn }}</el-breadcrumb-item>
            <el-breadcrumb-item v-if="route.path!='/homes'"> {{ breadcrumbData.secondLevelColumn }}</el-breadcrumb-item>
        </el-breadcrumb> 
   </el-card>
           <el-card shadow="never" class="main-content"> 
           
           <router-view v-if="route.path!='/homes'"></router-view> 
           <div v-else>
                  欢迎进入电商网站后台管理系统
           </div>
           </el-card>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { onBeforeMount, ref } from "vue";
import axios from "../axios";
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from "@element-plus/icons-vue";

import {
  PieChartOutlined,
  ShoppingOutlined,
  UserOutlined,
  SettingOutlined,
  FileOutlined,
} from "@ant-design/icons-vue";
import { useRouter,useRoute } from "vue-router";
const router=useRouter();
const route=useRoute();
const breadcrumbData=ref({
  firstLevelColumn:'',
  secondLevelColumn:''
})
const setBreadcrum=(path)=>{
  if(path==='/home/addgoods') return breadcrumbData.value={
    firstLevelColumn:'商品管理',
    secondLevelColumn:'添加商品'
  }
  console.log(path)
  const pathArr=path.split('/')
  console.log(pathArr[1]);
  menus.value.forEach(item=>{
    const subItem=item.children.find(subItem=>subItem.path==pathArr[2])//利用find函数在数组寻找符合条件的路径
    if(subItem){
      breadcrumbData.value.firstLevelColumn=item.authName;
      breadcrumbData.value.secondLevelColumn=subItem.authName
      return
    }
  })
}
//用户名
const username=window.localStorage.getItem('username');
// 图标
const icon = {
  125: PieChartOutlined,
  103: ShoppingOutlined,
  101: UserOutlined,
  102: SettingOutlined,
  145: FileOutlined,
};

const handleOpen = (key, keyPath) => {
  console.log(key, keyPath);
};
const handleClose = (key, keyPath) => {
  console.log(key, keyPath);
};

// 保存menu的数组
const menus = ref([]);
// 载入菜单
onBeforeMount(async () => {
  const { data: res } = await axios.get("menus");
  console.log(res);
  console.log(route.path)
  menus.value = res.data;

  setBreadcrum(route.path)
});

const logout=()=>{
  window.localStorage.removeItem('token');
  window.localStorage.removeItem('username');
  router.push('/login')
}
</script>

<style scoped>
.el-container {
  height: 100vh;
}

.el-aside {
  background: #2f63b8;
  color: white;
}

.el-aside > h4 {
  text-align: center;
}

.el-header {
  border-bottom: 2px solid #ededed;
}

.el-main {
  background: #f9f9f9;
}

.el-header{
  border-bottom: 2px solid #ededed;
  display: flex;
  align-items: center;
  justify-content: end;
}

.el-header>span{
  margin-right: 15px;
}

.main-content{
  margin-top: 20px;
  min-height: 360px;
}


.el-menu-item span{
  margin-left: 12px;
}
</style>
